|
|
|
|
|
|
|
Like the other Add methods discussed so far, it can return a reference to the object just created, as shown here: |
|
|
|
|
|
|
|
|
Dim tempNode As Node
Set tempNode = tv1.Nodes.Add(, , "mykey", "Test Node")
tempNode.Image = 1
tempNode.SelectedImage = 2
tempNode.ExpandedImage = 3 |
|
|
|
|
|
|
|
|
The TreeView control presents a "collapsible" view of the tree structure, as shown in Figure 12.11. |
|
|
|
|
|
|
|
|
Figure 12.11
This simple tree structure was
created using the TreeView control. |
|
|
|
|
|
|
|
|
In Figure 12.11, notice the use of plus, minus, lines, and images to indicate which parts of the tree are "expanded" and which are "collapsed." Users can manipulate the tree with the mouse, expanding and collapsing its branches as desired. For the programmer, the key to getting the items to appear where you want them is understanding the relationships between them. |
|
|
|
|
|
|
|
|
By using the SingleSel property, you can make the tree automatically expand a node when users select it.  |
|
|
|
|
|
|
|
|
Understanding the Root Property |
|
|
|
|
|
|
|
|
At the very top of a tree structure is its root. A root is the node from which all other nodes descend. A tree has only one root node. The tree pictured in Figure 12.11 is a simple organizational chart for a small company. The node called "President of the Company" is the root node, simply because it was the first one added to the TreeView control. |
|
|
|
|
|
|
|
|
Part of what defines any node in a tree is its root. Therefore, each node in the Nodes collection has a Root property that refers to the tree's root node. For the tree pictured in Figure 12.11, you can use the following code to verify that every node has the same root: |
|
|
|
|
|
|
|
|
Dim tempNode As Node
For Each tempNode in tv1.Nodes
Print tempNode.Text & "'s root is " & tempnode.Root.Text
Next tempNode |
|
|
|
|
|
|
|
|
Note that Root is both a property of a node and a node itself. |
|
|
|
|
|
|
|
|
Working with the Parent Property |
|
|
|
|
|
|
|
|
You have just seen that every node in the Nodes collection can access the root. However, just knowing the root of a node is not enough to define a position in the tree hierarchy. |
|
|
|
|
|